package inventario;

import conexion.CConexion;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CInvCd100 extends CConexion {

    String compania = null;

    public CInvCd100() {
        super();
        compania = login.getCompaniaActual();
    }

    public ResultSet buscarProducto(String cdgo, String desc, String copr, String pr, String area) throws SQLException {
        String sqlBuscarProducto = "select pr100nopr, cd100cdgo, cd100desc, sum(invexi100.exi100sact) as total from pr100, invcd100, invexi100 where cd100nocia = '" + compania + "' and cd100nocia=exi100nocia and pr100nocia = cd100nocia and pr100copr = cd100cdpr and cd100cdgo=exi100cdgo and exi100sact<>0 ";
        if (!copr.isEmpty()) {
            sqlBuscarProducto += " and pr100copr = '" + copr + "'";
        }
        if (!pr.isEmpty()) {
            sqlBuscarProducto += " and pr100nopr like '%" + pr + "%'";
        }
        if (!cdgo.isEmpty()) {
            sqlBuscarProducto += " and cd100cdgo like '%" + cdgo + "%'";
        }
        if (!desc.isEmpty()) {
            sqlBuscarProducto += " and cd100desc like '%" + desc + "%'";
        }
        if (!area.isEmpty()) {
            sqlBuscarProducto += " and cd100area = '" + area + "'";
        }
        sqlBuscarProducto += " group by cd100cdgo order by pr100nopr";
        Statement stncBuscarProducto = this.getConexion().createStatement();
        ResultSet rsBuscarProducto = stncBuscarProducto.executeQuery(sqlBuscarProducto);
        return rsBuscarProducto;
    }

    public ResultSet buscarProductoPorCodigo(String codigoProducto) throws SQLException {
        String sqlBuscarProducto = "select * from invcd100 where cd100nocia = '" + compania + "' and cd100cdgo = '" + codigoProducto + "'";
        Statement stncBuscarProducto = this.getConexion().createStatement();
        ResultSet rsBuscarProducto = stncBuscarProducto.executeQuery(sqlBuscarProducto);
        return rsBuscarProducto;
    }

    public void actualizarCostos(String codigoProducto, Double costoAnterior, Double costoPromedio, Double costoUnitario) throws SQLException {
        String sqlCostos = "update invcd100 set cd100coan = '" + costoAnterior + "', cd100copr = '" + costoPromedio + "', cd100coul = '" + costoUnitario + "' where cd100nocia = '" + compania + "' and cd100cdgo = '" + codigoProducto + "'";
        Statement stncCostos = this.getConexion().createStatement();
        stncCostos.executeUpdate(sqlCostos);
        stncCostos.close();
    }
}